﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using BusinessLogicLayer;

namespace ChamCong.Admin
{
    public partial class TinhLuong : BasePage
    {
        NhanVienBLL nvBLL = new NhanVienBLL();
        TongCongThangBLL tctBLL = new TongCongThangBLL();
        ThamSoBLL tsBLL = new ThamSoBLL();
        CT_PhuCapBLL ctpcBLL = new CT_PhuCapBLL();
        CT_LuongThangBLL ctLBLL = new CT_LuongThangBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtNam.Text = DateTime.Now.Year.ToString();
                drThang.SelectedValue = DateTime.Now.Month.ToString();
            }
        }

        protected void btnTinhLuong_Click(object sender, EventArgs e)
        {
            int _thang = int.Parse(drThang.SelectedValue);
            int _nam = int.Parse(txtNam.Text);
            if (tctBLL.KiemTraTongCong(_thang, _nam) == false)
            {
                sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Error, "Thông báo","Chưa chốt công!");
                return;
            }
            else
            {
                //Tính lương
                if (ctLBLL.KiemTraTinhLuong(_thang, _nam) == false)
                {
                    List<NHANVIEN> lsNV = nvBLL.LayTatCa();

                    List<TONGCONGTHANG> lsTC = tctBLL.LayDSTongCong(_thang, _nam);

                    int _Congtc = int.Parse(tsBLL.LayGiaTri("TCThang"));

                    int TLCoInLu = int.Parse(tsBLL.LayGiaTri("TLCoInLu"));

                    int TLHSInLu = 100 - TLCoInLu;

                    foreach (var a in lsNV)
                    {
                        CT_LUONGTHANG ctL = new CT_LUONGTHANG();
                        ctL.MaNV = a.MaNV;
                        ctL.Thang = _thang;
                        ctL.Nam = _nam;
                        foreach (var b in lsTC)
                        {
                            if (a.MaNV == b.MaNV)
                            {
                                if (b.CongLamViec == null)
                                {
                                    b.CongLamViec = 0;
                                }

                                if (b.HieuSuatLamViec == null)
                                {
                                    b.HieuSuatLamViec = 0;
                                }

                                if (a.MucLuong == null)
                                {
                                    a.MucLuong = 0;
                                }
                                //int y = (int)(b.CongLamViec * a.MucLuong * TLCoInLu);
                                Double x = (Double)((b.CongLamViec * TLCoInLu) / (_Congtc * 100)) + (Double)((b.HieuSuatLamViec * TLHSInLu) / (100 * 100));
                                //int LuongCong = (int)(b.CongLamViec * a.MucLuong * TLCoInLu) / (_Congtc * 100);
                                //int LuongHS = (int)(a.MucLuong * b.HieuSuatLamViec * TLHSInLu) / (100 * 100);

                                ctL.Luong = (int)(a.MucLuong * x);

                                ctL.LuongTangCa = (int)(b.CongTangCa * a.MucLuong) / _Congtc;
                                //Tính lương phụ cấp
                                List<int> pc = ctpcBLL.LayTienPCThang(a.MaNV, _thang, _nam);
                                int tienphucap = 0;
                                foreach (var c in pc)
                                {
                                    tienphucap += c;
                                }

                                ctL.PhuCap = tienphucap;
                                int tongluong = (int)(ctL.Luong + ctL.LuongTangCa + tienphucap);

                                ctL.TongLuongThang = LamTronLuong(tongluong);

                                ctLBLL.Them(ctL);
                                goto xong;
                            }
                        }
                    xong: { }
                    }
                    sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Success, "Thông báo", "Tính Lương Thành Công!");
                }
                else
                {
                    sbcMsgBox1.SetTitleText(SBWebControls.sbcMsgBox.eMessageType.Info, "Thông báo", "Đã Tính Lương Trước Đây!");
                }

                //List<NHANVIEN> lsNVx = nvBLL.LayTatCa();

                gvBangCong.DataSource = ctLBLL.LayBangLuong(_thang, _nam, 0, "");
                gvBangCong.DataBind();

            }
        }

        protected int LamTronLuong(int inLuong)
        {
            double Tien = inLuong / 5000;
            return (int)Math.Round(Tien, 0) * 5000;
        }

    }
}